package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.home.GoodsDetailModel;
import com.luck.easyfoodbatch.mvp.home.GoodsDetailPresenter;
import com.luck.easyfoodbatch.mvp.home.GoodsDetailView;
import com.luck.easyfoodbatch.mvp.home.GoodsInfoModel;
import com.luck.easyfoodbatch.mvp.home.GoodsInfoPresenter;
import com.luck.easyfoodbatch.mvp.home.GoodsInfoView;
import com.luck.easyfoodbatch.mvp.model.GoodDetail;
import com.luck.easyfoodbatch.mvp.model.GoodModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;

public class FragmentShopMoreViewPager extends BaseFragment implements GoodsDetailView, GoodsInfoView {
    private LinkedHashMap<String, Fragment> fragments;
    private ProductDetailsFragment detailsFragment;
    private ParametersFragment parametersFragment;
    private EvaluationFragment evaluationFragment;
    private String gid = "";
    private GoodsDetailModel detailModel;
    private GoodsInfoModel infoModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_shop_viewpager, container, false);
        ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
        gid = getActivity().getIntent().getStringExtra("gid");
        String p_id = (String) SharedPreferencesUtil.getData(getActivity(), HttpUtil.P_ID, "3");
        detailModel = new GoodsDetailPresenter(this, mContext);
        detailModel.goodDetails(1, gid);
        infoModel = new GoodsInfoPresenter(this, mContext);
        infoModel.goodsInfo(gid, p_id, 1);
        detailsFragment = new ProductDetailsFragment();
        parametersFragment = new ParametersFragment();
        evaluationFragment = new EvaluationFragment();
        TabLayout tab = (TabLayout) rootView.findViewById(R.id.tab);
        viewPager.setAdapter(new MyPagerAdapter(getFragmentManager()));
        viewPager.setOffscreenPageLimit(0);
        tab.setupWithViewPager(viewPager);
        return rootView;
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<String> titles;

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            fragments = new LinkedHashMap<>();
            fragments.put("商品详情", detailsFragment);
            fragments.put("商品参数", parametersFragment);
            fragments.put("商品评价", evaluationFragment);
            titles = new ArrayList<>();
            titles.addAll(fragments.keySet());
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(titles.get(position));
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

    }

    @Override
    public void getGoodsDetailSuccess(GoodDetail model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                GoodDetail.DataBean data = model.getData();
                if (detailsFragment != null) {
                    detailsFragment.initTextData(data);
                    parametersFragment.initDataView(data);
                }
            }
        }
    }

    @Override
    public void getGoodInfoSuccess(GoodModel model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                GoodModel.DataBean data = model.getData();
                if (parametersFragment != null) {
                    parametersFragment.initData(data);
                }
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        // hideShowProgress();
    }
}
